//
//  AFNetworkTools.swift
//  AFN网络封装
//
//  Created by vane on 16/8/10.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import AFNetworking
enum AFNetworkToolsMethod: String {

    case GET = "GET"

    case POST = "POST"
}
class AFNetworkTools: AFHTTPSessionManager {

    static let sharedTools:AFNetworkTools = {
       
        let tools = AFNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        return tools
        
    }()
    
    func request(method: AFNetworkToolsMethod, urlString: String, parameters: AnyObject?, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // get请求
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in

                success(response: response)
                }, failure: { (_, error) -> Void in

                    failure(error: error)
            })
            
        }else {
            // post 请求
            POST(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in

                success(response: response)
                }, failure: { (_, error) -> Void in

                    failure(error: error)
            })
            
        }
        
    }

    
}
extension AFNetworkTools{

    func composeUpdate(status:String,success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        
        let url = "https://api.weibo.com/2/statuses/update.json"
        
        let params = [
            
            "access_token":GYUserAccountViewModel.sharedTools.accessToken!,
            "status":status
        ]
        
        request(AFNetworkToolsMethod.POST, urlString: url, parameters: params, success: success, failure: failure)
        
    }
    
    func composeUpload(status: String, images:[UIImage], success:(response: AnyObject?)->(), failure:(error: NSError)->()){

        let urlStr = "https://upload.api.weibo.com/2/statuses/upload.json"

        let params = [
            "access_token": GYUserAccountViewModel.sharedTools.accessToken!,
            "status": status
        ]
        
        POST(urlStr, parameters: params, constructingBodyWithBlock: { (formdata) -> Void in
            
            let data = UIImagePNGRepresentation(images.first!)!
            
            formdata.appendPartWithFileData(data, name: "pic", fileName: "picture", mimeType: "application/octet-stream")
            
            }, progress: nil, success: { (_, response) -> Void in
                success(response: response)

            }, failure: { (_, error) -> Void in
                failure(error: error)

        })
    }


}
//MARK: - 微博首页数据
extension AFNetworkTools{
    func homeLoadData(sinceId:Int64, maxId:Int64,success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求的urlString
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params = [
            "access_token": GYUserAccountViewModel.sharedTools.userAccount!.access_token!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"

        ]
        request(AFNetworkToolsMethod.GET, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
}
//MARK: - 授权界面接口

extension AFNetworkTools {
    
    func oauthLoadUserAccount(code:String,success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        
        let url = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = [
            "client_id":WBAppKey,
            "client_secret":WBAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":WBRedirect_uri
        ]
        self.request(AFNetworkToolsMethod.POST, urlString: url, parameters: parameters, success: success, failure: failure)
        
    }
    
    func oauthLoadUserInfo(accessToken:GYAccessTokenModel,success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        
        let url = "https://api.weibo.com/2/users/show.json"
        
        let param = [
            
            "access_token":accessToken.access_token!,
            "uid":accessToken.uid!
        ]
        
        self.request(AFNetworkToolsMethod.GET, urlString: url, parameters: param, success: success, failure: failure)
    }

}
